package cn.interestingshop.dao;

import cn.interestingshop.entity.UserAddress;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserAddressDaoImpl extends BaseDaoImpl implements UserAddressDao{
    public UserAddressDaoImpl(Connection connection) {
        super(connection);
    }

    @Override
    public List<UserAddress> createEntityByResultSet(ResultSet rs) throws Exception {

        List<UserAddress> list=new ArrayList<>();

        while (rs.next()){

            UserAddress userAddress=new UserAddress();
            userAddress.setAddress(rs.getString("address"));
            userAddress.setCreateTime(rs.getString("create_time"));
            userAddress.setId(rs.getInt("id"));
            userAddress.setRemark(rs.getString("remark"));
            userAddress.setUserId(rs.getInt("user_id"));

            list.add(userAddress);
        }
        return list;
    }

    public UserAddress createEntityByResultSet2(ResultSet rs) throws Exception {

        UserAddress userAddress=new UserAddress();

        while (rs.next()){


            userAddress.setAddress(rs.getString("address"));
            userAddress.setCreateTime(rs.getString("create_time"));
            userAddress.setId(rs.getInt("id"));
            userAddress.setRemark(rs.getString("remark"));
            userAddress.setUserId(rs.getInt("user_id"));


        }
        return userAddress;
    }

    @Override
    public List<UserAddress> selectOne(int userId) {
        ResultSet rs=null;
        List<UserAddress> list=new ArrayList<>();

        String sql="SELECT id,user_id,address,create_time,is_default,remark from t_user_address where user_id="+userId;
        rs=executeQuery(sql,null);
        try {
            list=createEntityByResultSet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }
        return list;
    }

    @Override
    public int insert(int userId,String address, String remark) {

        int rs=0;

        try {
            String sql = "INSERT INTO `interesting_shop_db`.`t_user_address`(`user_id`, `address`, `remark`) VALUES (?,?,?)";
            Object[] objects = new Object[]{userId, address, remark};
            rs = executeInsert(sql, objects);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeResource();
        }

        return rs;
    }

    @Override
    public UserAddress selectOneid(int id) {
        ResultSet rs=null;
        UserAddress userAddress=new UserAddress();

        String sql="SELECT id,user_id,address,create_time,is_default,remark from t_user_address where id="+id;
        rs=executeQuery(sql,null);
        try {
            userAddress=createEntityByResultSet2(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            closeResource();
            closeResource(rs);
        }
        return userAddress;
    }
}
